package com.company.doubleIndex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LongPressedName {
    public boolean isLongPressedName(String name, String typed) {
        // 判断条件包括 1、字符 及其顺序相同 2、typed 中字符出现的次数不少于name中出现的次数
        return checkSquence(name, typed);
    }

    private boolean checkSquence(String name,String typed){

        char[] nameCharArray = name.toCharArray();
        char[] typedCharArray = typed.toCharArray();


        int indexInName=0;
        int indexInTyped=0;

        int currentCharCountInName = 0;
        int currentCharCountInTyped = 0;
        while (indexInName<nameCharArray.length&&indexInTyped<typedCharArray.length){
            //将name 指到当前连续相同字符的最后一个元素
            if (indexInName<nameCharArray.length-1&&nameCharArray[indexInName+1] == nameCharArray[indexInName]) {
                currentCharCountInName++;
                indexInName++;
                continue;
            }
            //将type 指向当前连续相同字符的最后一个元素
            if (indexInTyped<typedCharArray.length-1&&typedCharArray[indexInTyped+1] == typedCharArray[indexInTyped]) {
                currentCharCountInTyped++;
                indexInTyped++;
                continue;
            }
            //判断元素是否相同
            if (nameCharArray[indexInName]!=typedCharArray[indexInTyped]) {
                return false;
            }
            //判断元素数量是否是 名字中的少
            if (currentCharCountInName>currentCharCountInTyped) {
                return false;
            }
            //判断通过 分别进入下一个字符
            indexInName++;
            indexInTyped++;
            currentCharCountInName = 0;
            currentCharCountInTyped = 0;
        }
        return indexInName==nameCharArray.length&&indexInTyped == typedCharArray.length;
    }
}
